package com.study.d030animation;

import javafx.animation.Animation.Status;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**时间线动画-同时进行旋转，缩放
*@description
*@author xtayhame
*date 2020年3月22日
*version
*since v1.0
*/
public class TimelineMain2 extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		HBox hBox = new HBox(20);
		Button b1=new Button("播放");
		Button b2=new Button("暂停");
		Button b3=new Button("停止");
		Button b4=new Button("设置加速");
		Button b7=new Button("从第二个点开始");

		Button b5=new Button("移动");

		Rectangle rectangle = new Rectangle(100, 100,Paint.valueOf("#EE82EE55"));

		hBox.getChildren().addAll(b1,b2,b3,b4,b7);
		anchorPane.getChildren().addAll(hBox,rectangle);

		AnchorPane.setTopAnchor(rectangle, 100.0);
		AnchorPane.setLeftAnchor(rectangle, 100.0);

		stage.setScene(new Scene(anchorPane, 800, 600));
		stage.show();

		//时间线动画
		Timeline timeline = new Timeline();
		//对矩形进行缩放,旋转
		KeyValue kv1SX = new KeyValue(rectangle.scaleXProperty(), 1);   //x轴缩放
		KeyValue kv1SY = new KeyValue(rectangle.scaleYProperty(), 1);	//y轴缩放
		KeyValue kv1R = new KeyValue(rectangle.rotateProperty(), 0);	//旋转
		KeyValue kv1X = new KeyValue(rectangle.translateXProperty(), 0);
		KeyValue kv1Y = new KeyValue(rectangle.translateYProperty(), 0);
		KeyFrame kf1 = new KeyFrame(Duration.seconds(0), kv1SX,kv1SY,kv1R,kv1X,kv1Y);

		KeyValue kv2SX = new KeyValue(rectangle.scaleXProperty(), 2);
		KeyValue kv2SY = new KeyValue(rectangle.scaleYProperty(), 2);
		KeyValue kv2R = new KeyValue(rectangle.rotateProperty(), 360);
		KeyValue kv2X = new KeyValue(rectangle.translateXProperty(), 300);
		KeyValue kv2Y = new KeyValue(rectangle.translateYProperty(), 0);
		KeyFrame kf2 = new KeyFrame(Duration.seconds(2), kv2SX,kv2SY,kv2R,kv2X,kv2Y);


		KeyValue kv3SX = new KeyValue(rectangle.scaleXProperty(), 1);
		KeyValue kv3SY = new KeyValue(rectangle.scaleYProperty(), 1);
		KeyValue kv3R = new KeyValue(rectangle.rotateProperty(), 0);
		KeyValue kv3X = new KeyValue(rectangle.translateXProperty(), 300);
		KeyValue kv3Y = new KeyValue(rectangle.translateYProperty(), 300);
		KeyFrame kf3 = new KeyFrame(Duration.seconds(4), kv3SX,kv3SY,kv3R,kv3X,kv3Y);

		timeline.getKeyFrames().addAll(kf1,kf2,kf3);
		timeline.setCycleCount(5);
		timeline.setAutoReverse(true);


		b1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				timeline.play();
			}
		});

	}



}
